$(function () {
    $(".product-overview").on("click",function () {
        let informationId = $(".directory").attr("information-id");
        console.log(informationId)
        //加密
        let aesKeyUtil = new Aes_key_util();
        const encryptedInformationId = aesKeyUtil.encrypt(informationId);
        window.location.href = "/www.xiaomi.com/product/details" +
            "?informationId=" +encodeURIComponent(encryptedInformationId)+
            "&commodityId=" +$(".directory").attr("commodity-id")+
            "&accountId=" +$(".directory").attr("account-id")+
            "&name="+$(".directory").attr("name")+
            "&menu=2"
    })
    $(".product-parameter").on("click",function () {
        let informationId = $(".directory").attr("information-id");
        //加密
        let aesKeyUtil = new Aes_key_util();
        const encryptedInformationId = aesKeyUtil.encrypt(informationId);
        window.location.href = "/www.xiaomi.com/product/details" +
            "?informationId=" +encodeURIComponent(encryptedInformationId)+
            "&commodityId=" +$(".directory").attr("commodity-id")+
            "&accountId=" +$(".directory").attr("account-id")+
            "&name="+$(".directory").attr("name")+
            "&menu=3"
    })
    $(".down").on("click", function () {
        $(".prompt").css({"display": "none"})
    })
    $(".redeem-codes").on("click",function () {
        let menu = $(".directory");

        let accountId = menu.attr("account-id");
        let name = menu.attr("name");

        //重要信息需要加密
        let informationId = menu.attr("information-id");
        //加密
        let aesKeyUtil = new Aes_key_util();
        const encryptedInformationId = aesKeyUtil.encrypt(informationId);
        console.log(encodeURIComponent(encryptedInformationId))
        window.open("/www.xiaomi.com/redeem/codes?informationId="+
            encodeURIComponent(encryptedInformationId)+"&accountId="+accountId+
            "&name="+name, "_blank");
    })
    $(".go-login").on("click",function () {
        window.location.href = "/www.xiaomi.com/login?menu=0"
    })
    let price = $(".price").attr("commodity-price");
    let discount = $(".price").attr("commodity-discount");
    if (parseInt(discount) === 6){
        let priceAfterDiscount = (parseFloat(price) * 0.6).toFixed(2);
        $(".price").text(priceAfterDiscount + "元")
        $(".price").attr("commodity-price",priceAfterDiscount)
    }else if (parseInt(discount) === 7){
        let priceAfterDiscount = (parseFloat(price) * 0.7).toFixed(2);
        $(".price").text(priceAfterDiscount + "元")
        $(".price").attr("commodity-price",priceAfterDiscount)
    }else if (parseInt(discount) === 8){
        let priceAfterDiscount = (parseFloat(price) * 0.8).toFixed(2);
        $(".price").text(priceAfterDiscount + "元")
        $(".price").attr("commodity-price",priceAfterDiscount)
    }else if (parseInt(discount) === 9){
        let priceAfterDiscount = (parseFloat(price) * 0.9).toFixed(2);
        $(".price").text( priceAfterDiscount+ "元")
        $(".price").attr("commodity-price",priceAfterDiscount)
    }else{
        let priceAfterDiscount = (parseFloat(price)).toFixed(2);
        $(".price").text(priceAfterDiscount + "元")
        $(".price").attr("commodity-price",priceAfterDiscount)
    }
    let index = 0;
    let imgs = $(".carousel").attr("imgs").split(",");
    console.log(imgs)
    $(".commodity-img").attr("src","http://localhost:8083/resource_packs/images/"+imgs[index])
    $(".carousel-img-right").on("click",function () {
        if ((index+1) !== imgs.length){
            index+=1;
        }else {
            index=0;
        }
        $(".commodity-img").attr("src","http://localhost:8083/resource_packs/images/"+imgs[index])
    })
    $(".carousel-img-left").on("click",function () {
        if ((index-1) < 0){
            index=(imgs.length-1);
        }else {
            index-=1;
        }
        $(".commodity-img").attr("src","http://localhost:8083/resource_packs/images/"+imgs[index])
    })
    $(".memory-box").on("click",function () {
        let rowBox = $($(this).parent()).find(".memory-box");
        rowBox.removeClass("specification-box")
        rowBox.find(".memory").removeClass("specification-title")

        $(this).addClass("specification-box")
        $($(this).find(".memory")).addClass("specification-title")
        total();
    })
    $("input.insurance").on("click",function () {
        total();
    })
    total();
    function total() {
        let addPrice = $(".specification-title");
        let price = $(".price");
        let totalSpecificationPrice = parseFloat(price.attr("commodity-price"));
        for (const addPriceElement of addPrice) {
            let inputUtil = new Input_util();
            console.log(!inputUtil.isEmpty($(addPriceElement).attr("add-price")))
            if (!inputUtil.isEmpty($(addPriceElement).attr("add-price"))){
                totalSpecificationPrice += parseFloat($(addPriceElement).attr("add-price"))
            }
        }
        console.log(totalSpecificationPrice)
        $(".specification-price").text(totalSpecificationPrice+"元")
        let insurance = $(".insurance:checked");
        $(".purchase-commodity-insurance").empty();
        for (const insuranceElement of insurance) {
            let addPrice = parseFloat($(insuranceElement).attr("add-price"));
            if (!isNaN(addPrice)){
                totalSpecificationPrice+=addPrice
                let insuranceName = $(insuranceElement).attr("insurance-name");
                $(".purchase-commodity-insurance").append(`<div><a>${insuranceName}</a><span>${addPrice}元</span></div>`)
            }
        }

        $(".total-price").text(totalSpecificationPrice+"元")
        $(".name-purchased-product").text($(".model").text()+" "+$(".specification-title").text())
    }
    $(".add-cart").on("click",function () {
        if (parseInt($(".directory").attr("account-id")) !== 0){
            let specificationBox = $(".specification-box");
            let insuranceBox = $(".insurance:checked");

            let specificationIds = "";
            let insuranceIds = "";
            //获取规格Id
            for (let i = 0; i < specificationBox.length; i++) {
                if (i !== 0){
                    specificationIds+=","+$(specificationBox[i]).attr("specification-id")
                }else{
                    specificationIds+=$(specificationBox[i]).attr("specification-id")
                }
            }
            //获取保险Id
            for (let i = 0; i < insuranceBox.length; i++) {
                if (i !== 0){
                    insuranceIds+=","+$(insuranceBox[i]).attr("insurance-id")
                }else{
                    insuranceIds+=$(insuranceBox[i]).attr("insurance-id")
                }
            }
            let shoppingCartJson = {
                informationId:$(".directory").attr("information-id"),
                commodityId:$(".directory").attr("commodity-id"),
                specificationIds:specificationIds,
                insuranceIds:insuranceIds
            }
            let shoppingCart = new Shopping_cart();
            shoppingCart.getShoppingCartByCommodityIdAndInformationId(shoppingCartJson).then(res=>{
                console.log(res)
                window.location.href ="/www.xiaomi.com/shopping/cart?accountId="+$(".directory").attr("account-id")
            })
        }else{
            window.location.href = "/www.xiaomi.com/login?menu=0"
        }
    })

})